<?php
//include error_reporting(0);
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
            $response = $this->getResponse();
            $response->setHeader('Content-Type', 'text/x-json');
        } else {
            $this->_helper->layout()->setLayout('build-layout2');
        }


        $projManager=new Managers\Projects();
        $projects=$projManager->getList(3,0,1);
        $this->view->projects=$projects;

        $menuManager=new Managers\Pages();
        $menu=$menuManager->getMenu();
        $this->view->menu=$menu;

    }

    public function indexAction()
    {
        $id = $this->_getParam("id");

        $PM = new \Managers\Pages();
        $page = $PM->getPage($id);

        $this->view->page=$page;
           $rotatorManager=new Managers\Rotator();
        $pictures=$rotatorManager->getList();
        $this->view->pictures=$pictures;


    }
     public function servicesAction()
    {
    }
     public function aboutusAction()
    {
    }
     public function contactusAction()
    {
          $this->view->status=$this->_getParam('send');
      if ($this->_request->isPost()) {



                $id = $this->_getParam('id');
                $email =$this->_getParam('email-guest');
        //            $email ='email@mail.ru';
                $subject =$this->_getParam('email-subject');
        //         $subject ='Beatiful subject';
              $message =$this->_getParam('email-message');
        //      $message ='ololo good message';
              $validator = new Zend_Validate_StringLength(array('min' => 3, 'max' => 256));
              $validator->isValid($email);
              $validator->isValid($message);
              $validator->isValid($subject);
              $mailManager=new \Managers\Mailsend();
              $mailManager->sendEmail($id,$subject,$message,$email);
               $this->_redirect(Zend_Registry::get('config')->resources->frontController->baseUrl."/index/contactus/send/1");
      }

    }
   
     public function pageAction()
    {
        $id = $this->_getParam("id");

        $PM = new \Managers\Pages();
        $page = $PM->getPage($id);

        $this->view->page=$page;




    }
    public  function allprojectsAction()
    {


          $projManager=new Managers\Projects();
        $newsperpage=5;
        $numberofprojects=$projManager->getTotalRecords();
        $page = intval($this->_request->getParam('page', 1));
          $pagprojects = null;
        if ($page > 0) {
            $offset = ($page - 1) *  $newsperpage;
            $pagprojects = $projManager->getList($newsperpage, $offset);
        } else {
            $pagprojects = $projManager->getList($newsperpage);
        }



        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($numberofprojects));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($newsperpage);

        $this->view->paginator = $paginator;
        $this->view->pagprojects=$pagprojects;


       


    }
    public function viewprojectAction()
    {
        $id=$this->_getParam("id");
       
    $projManager=new Managers\Projects();

        $oneproject=$projManager->getOne($id);
        $this->view->oneproject=$oneproject;


    }

    public function reviewsAction(){

        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();

            $response = $this->getResponse();
            $response->setHeader('Content-Type', 'text/x-json');

        }
         $id = $this->_getParam("id");

        $PM = new \Managers\Pages();
        $page = $PM->getPage($id);

        $this->view->page=$page;




        $reviewsManager=new Managers\Reviews();
        $list=$reviewsManager->getList();
        $this->view->list=$list;

         if ($this->_request->isPost()) {
            
            $nick=$this->_getParam('nickname');
            $comment=$this->_getParam('comment');

            $price=$this->_getParam('price');
            $quality=$this->_getParam('quality');
            $respons=$this->_getParam('responsiveness');
            $punctuality=$this->_getParam('punctuality');
            $professionalism=$this->_getParam('professionalism');
            $reviewsManager=new Managers\Reviews();
            $reviewsManager->addComment($nick,$comment,$price,$quality,$respons,$punctuality,$professionalism);

            $list=$reviewsManager->getList();
            $this->view->data = $list;
             $this->renderScript('/ajax/json.phtml');

        }

    }

    public function galleryAction(){

        $galleryManager=new Managers\Gallery();
        $photos=$galleryManager->getAll();
        $this->view->photos=$photos;
        
    }

    public function calculateAction(){
      $this->_helper->layout()->disableLayout();

      if ($this->_request->isPost())
        {
               $string=$this->_getParam('counter');

             
             
            // Choose members for calculation
               $countTrue=preg_match("~^(\d+[.]?\d+|\d)([+\-*])(\d+[.]?\d+|\d)~",$string,$members);


//               print_r($members);

               if ($countTrue)
               {
               $member1=$members[1]; //First member
               $sign=$members[2]; //Sign of operation
               $member2=$members[3]; //Second member

                       switch ($sign)
                              {
                                 case "+":
                                 $result=$member1+$member2;
                                 break;

                                 case "-":
                                 $result=$member1-$member2;
                                 break;

                                case "*":
                                 $result=$member1*$member2;
                                 break;

                                case "/":
                                 $result=$member1/$member2;
                                 break;

                               


                                default:
                                 $result=0;

                               }


                         
                         $output=$member1."$sign".$member2."=".$result;
                  

                         if (($sign=="/")&&($member2==0))
                         $output="Division by zero";
                         $this->view->output=$output;
                          $calcManager=new Managers\Calculate();
                         $idCalc=$calcManager->addCalc("$output");
                         
                         $calcManager->addUser($_SERVER["REMOTE_ADDR"],$idCalc);

                         
               }
               

               else {$this->view->output="Enter the correct data";}

//       print "M1:".$member1."<br>";
//       print "M2:".$member2."<br>";



         
         }

    }

    public function viewcalculationAction(){
      $this->_helper->layout()->disableLayout();
      $calcManager=new Managers\Calculate();
      $all=$calcManager->getList();
        $this->view->all=$all;
       

 }
}